<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\common\service\SysLogService;
class Task extends Controller
{
    public function getTaskList()
    {

        $data = request()->post();
        $page = isset($data['page']) ? intval($data['page']) : 1;
        $limit = isset($data['limit']) ? intval($data['limit']) : 10;
        
        $map = [];
        $map['is_delete'] = ['=', '0'];
        
        // 如果有搜索条件，可以在这里添加
        if (isset($data['name']) && !empty($data['name'])) {
            $map['name'] = ['like', '%' . $data['name'] . '%'];
        }
        

        
        if (isset($data['status']) && $data['status'] !== '') {
            $map['status'] = ['=', intval($data['status'])];
        } 
        
        // 获取总数
        $count = Db::table('zstp_zip_task')
            ->where($map)
            ->count();
            
            
        // 获取分页数据
        $list = Db::table('zstp_zip_task')
            ->where($map)
            ->page($page, $limit)
            ->order('create_time desc')
            ->select();


        foreach($list as $key => $value){
           //查任务一共几篇
           $mapResult['task_id'] = $value['id'];
           $mapResult['is_delete'] = 0;
           $countq = Db::table('zstp_zip_queue')
           ->where($mapResult)
           ->count();
        

           $mapResult['status'] = 1;
           $countResult = Db::table('zstp_zip_queue')
           ->where($mapResult)
           ->count();

           if($countq == 0){
            $list[$key]['progress'] = 0;
           }else{
            $progress = $countResult/$countq*100;
            $list[$key]['progress'] = round($progress,2);
           } 
        }
            
        return json([
            'code' => 200,
            'message' => '获取成功',
            'data' => [
                'total' => $count,
                'items' => $list,
                'page' => $page,
                'limit' => $limit
            ]
        ]);
        
    }



    public function deletetasklist(){
        $data = request()->post();
        $id = isset($data['id']) ? intval($data['id']) : 0;
        if($id == 0){
            return json([
                'code' => 400,
                'message' => '参数错误'
            ]);
        }

        $map['id'] = $id;
        Db::table('zstp_zip_task')->where($map)->update([
            'is_delete' => 1,
            'update_time' => date('Y-m-d H:i:s')
        ]);

        Db::table('zstp_zip_queue')->where($map)->update([
            'is_delete' => 1,
            'update_time' => date('Y-m-d H:i:s')
        ]);

        $sysLogService = new SysLogService();

        $taskName = Db::table('zstp_zip_task')->where('id', $id)->value('name');

        $result =  $sysLogService->addLog('删除任务('. $taskName .')', $id, 3, '删除任务id:'.$id);

        return json([
            'code' => 200,
            'message' => '删除成功'
        ]);
    }

    public function batchDeletetasklist(){
        $data = request()->post();
        $ids = isset($data['ids']) ? $data['ids'] : [];
        if(empty($ids)){
            return json([
                'code' => 400,
                'message' => '参数错误'
            ]);
        }

        $map['id'] = ['in', $ids];
        $map['is_delete'] = 0;

        Db::table('zstp_zip_task')->where($map)->update([
            'is_delete' => 1,
            'update_time' => date('Y-m-d H:i:s')
        ]);

        Db::table('zstp_zip_queue')->where($map)->update([
            'is_delete' => 1,
            'update_time' => date('Y-m-d H:i:s')
        ]);
        
        $sysLogService = new SysLogService();
        $result =  $sysLogService->addLog('删除任务id('. $ids.')', $ids, 3, '删除任务id:'.$ids);

        return json([
            'code' => 200,
            'message' => '删除成功'
        ]);
    }


    public function retryFailedItems(){
        $data = request()->post();


        $map['task_id'] = $data['id'];
        $map['is_delete'] = 0;
        $map['status'] = array("<>", 1);

        $result = Db::table('zstp_zip_queue')->where($map)->update([
            'status' => 0,
            'update_time' => date('Y-m-d H:i:s')
        ]);

        $sysLogService = new SysLogService();

        $taskName = Db::table('zstp_zip_task')->where('id', $data['id'])->value('name');

        $result =  $sysLogService->addLog('重试任务id('.$taskName .')', $data['id'], 4, '重试任务id:'.$data['id']);

        return json([
            'code' => 200,
            'message' => '重试成功'
        ]);

    }     
}